<template>
  <div class="login-container">
    <div class="login-form-content">
      <div class="login-bg">
        <img
          src="../../assets/login-bg.png"
          alt=""
        >
      </div>
      <div class="s-d-flex s-ai-center form-rright">
        <div class="w100p">
          <div class="title-container">
            <h2 class="title">欢迎登录</h2>
          </div>
          <el-tabs
            v-model="activeName"
            @tab-click="handleClick"
          >
            <el-tab-pane
              label="账号登录"
              name="first"
            >
              <el-form
                ref="loginForm"
                :model="loginForm"
                size="small"
                :rules="loginRules"
                class="login-form"
                label-position="left"
              >

                <el-form-item
                  prop="username"
                  class="el-form-item-1"
                >
                  <span class="svg-container">
                    <img
                      src="../../assets/user01.png"
                      class="w16 h16 s-posr t-5"
                      alt=""
                    >
                    <!-- <svg-icon icon-class="user" /> -->
                  </span>
                  <el-input
                    ref="username"
                    v-model="loginForm.username"
                    placeholder="账号"
                    name="username"
                    type="text"
                    tabindex="1"
                    auto-complete="off"
                  />
                </el-form-item>

                <el-form-item
                  prop="password"
                  class="el-form-item-1"
                >
                  <span class="svg-container">
                    <img
                      src="../../assets/pwd.png"
                      class="w14 h16 s-posr t-5"
                      alt=""
                    >
                    <!-- <svg-icon icon-class="password" /> -->
                  </span>
                  <el-input
                    :key="passwordType"
                    ref="password"
                    v-model="loginForm.password"
                    placeholder="密码"
                    name="password"
                    tabindex="2"
                    :type="passwordType"
                    auto-complete="off"
                    @keyup.enter.native="handleLogin"
                  />
                  <span
                    class="show-pwd"
                    @click="showPwd"
                  >
                    <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
                  </span>
                </el-form-item>
                <el-form-item>
                  <div class="s-d-flex s-jc-space-between">
                    <div class="color-gray">
                      <el-checkbox v-model="checked">记住密码</el-checkbox>
                    </div>
                    <div
                      class="s-ta color-grayr s-pointer"
                      @click="handleForgetFace"
                    >
                      忘记密码?
                    </div>
                  </div>
                </el-form-item>
                <el-button
                  :loading="loading"
                  type="primary"
                  style="width:100%;margin-bottom:30px;background:#34BBFF;border-color:#34BBFF"
                  @click.native.prevent="handleLogin"
                >登录</el-button>
                <div
                  class="color-gray s-tac s-pointer"
                  @click="handleRegister"
                >
                  申请注册
                </div>
              </el-form>
            </el-tab-pane>
            <el-tab-pane
              label="验证码登录"
              name="second"
            >
              <el-form
                ref="phoneLoginForm"
                :model="phoneLoginForm"
                size="small"
                :rules="loginPhoneRules"
                class="login-form"
                label-position="left"
              >

                <el-form-item
                  prop="phone"
                  class="el-form-item-1"
                >
                  <span class="svg-container">
                    <img
                      src="../../assets/user01.png"
                      class="w16 h16 s-posr t-5"
                      alt=""
                    >
                    <!-- <svg-icon icon-class="user" /> -->
                  </span>
                  <el-input
                    v-model="phoneLoginForm.phone"
                    placeholder="手机号"
                    auto-complete="off"
                  />
                </el-form-item>

                <el-form-item
                  prop="smsCode"
                  class="el-form-item-1"
                >
                  <span class="svg-container">
                    <img
                      src="../../assets/pwd.png"
                      class="w14 h16 s-posr t-5"
                      alt=""
                    >
                    <!-- <svg-icon icon-class="password" /> -->
                  </span>
                  <el-input
                    v-model="phoneLoginForm.smsCode"
                    placeholder="验证码"
                    auto-complete="off"
                  />
                  <span
                    class="show-pwd-code fz14"
                    @click="getCode"
                  >
                    {{showTime?`${timer}s重新获取验证码`:'获取验证码'}}
                  </span>
                </el-form-item>

                <el-button
                  :loading="loading"
                  type="primary"
                  style="width:100%;margin-bottom:30px;background:#34BBFF;border-color:#34BBFF"
                  @click.native.prevent="handleLoginPhone()"
                >登录</el-button>
                <div
                  class="color-gray s-tac s-pointer"
                  @click="handleRegister"
                >
                  申请注册
                </div>
              </el-form>
            </el-tab-pane>

          </el-tabs>
        </div>

      </div>
    </div>

  </div>
</template>

<script>
import { validUsername } from "@/utils/validate";
import md5 from "md5";
import * as userApi from "@/api/user";
export default {
  name: "Login",
  data() {
    const validateUsername = (rule, value, callback) => {
      if (!value) {
        callback(new Error("请填写账号"));
      } else {
        callback();
      }
    };
    const validatePassword = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error("请输入正确的6~12位密码"));
      } else {
        callback();
      }
    };
    const validateUserPhone = (rule, value, callback) => {
      if (!value) {
        callback(new Error("请输入手机号"));
      } else if (value && !/^1(3|4|5|6|7|8|9)\d{9}$/.test(value)) {
        callback(new Error("电话有误，请重填"));
      } else {
        callback();
      }
    };
    const validateVerificationCode = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error("请输入验证码"));
      } else {
        callback();
      }
    };
    return {
      activeName: "first",

      loginForm: {
        username: "",
        password: "",
      },
      passwordType: "password",
      loginRules: {
        username: [
          { required: true, trigger: "blur", validator: validateUsername },
        ],
        password: [
          { required: true, trigger: "blur", validator: validatePassword },
        ],
      },
      phoneLoginForm: {
        phone: "",
        smsCode: "",
      },
      loginPhoneRules: {
        phone: [
          { required: true, trigger: "blur", validator: validateUserPhone },
        ],
        smsCode: [{ required: true, trigger: "blur", message: "请输入验证码" }],
      },
      loading: false,
      redirect: undefined,
      activeName: "first",
      checked: false,
      showTime: false,
      timer: 60,
    };
  },
  computed: {
    MD5Model() {
      return {
        username: this.loginForm.username,
        password: md5(this.loginForm.password),
      };
    },
  },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true,
    },
  },
  created() {
    this.checked = sessionStorage.getItem("remember") ? true : false;
    if (this.checked) {
      this.loginForm = sessionStorage.getItem("remember")
        ? JSON.parse(sessionStorage.getItem("remember"))
        : {
            username: "",
            password: "",
          };
    }
  },
  methods: {
    handleClick(val) {
      this.activeName = val.name;
    },
    showPwd() {
      if (this.passwordType === "password") {
        this.passwordType = "";
      } else {
        this.passwordType = "password";
      }
      this.$nextTick(() => {
        this.$refs.password.focus();
      });
    },
    //获取验证码
    getCode() {
      if(this.showTime) {
        return
      }
      if (!this.phoneLoginForm.phone) {
        this.$message.error({
          message: "请填写账号",
        });
        return;
      } else if (!/^1(3|4|5|6|7|8|9)\d{9}$/.test(this.phoneLoginForm.phone)) {
        this.$message.error({
          message: "电话有误，请重填",
        });
        return;
      }
      this.timer = 60;
      this.showTime = true;
      let timer = null;
      if (timer) {
        clearInterval(timer);
      }
      userApi.sendMessage({ phone: this.phoneLoginForm.phone }).then((res) => {
        if (res && res.success) {
          timer = setInterval(() => {
            if (this.timer <= 0) {
              this.showTime = false;
              clearInterval(timer);
            } else {
              this.timer--;
            }
          }, 1000);
        }
      });
    },
    handleLogin() {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          this.loading = true;
          if (this.checked) {
            sessionStorage.setItem("remember", JSON.stringify(this.loginForm));
          } else {
            sessionStorage.setItem("remember", "");
          }
          this.$store
            .dispatch("user/login", this.loginForm)
            .then(() => {
              this.$router.push({ path: this.redirect || "/" });
              this.loading = false;
            })
            .catch(() => {
              this.loading = false;
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    //申请注册
    handleRegister() {
      this.$router.push({
        path: "/register",
      });
    },
    //手机账号登入
    handleLoginPhone() {
      this.$refs.phoneLoginForm.validate((valid) => {
        if (valid) {
          this.loading = true;
          this.$store
            .dispatch("user/loginPhone", this.phoneLoginForm)
            .then(() => {
              this.$router.push({ path: this.redirect || "/" });
              this.loading = false;
            })
            .catch(() => {
              this.loading = false;
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    //忘记密码
    handleForgetFace() {
      this.$router.push({
        path: "/forgetFace",
      });
    },
  },
};
</script>

<style lang="scss" scoped>
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg: #283443;
$light_gray: #fff;
$cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}
/* reset element-ui css */
.login-container {
  .el-tabs__item {
    color: #fff;
  }
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;
    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 47px;
      caret-color: $cursor;
      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .el-form-item-1 {
    border: 1px solid #d7dadb;
    background: #fff;
    border-radius: 2px;
    height: 40px;
    line-height: 40px;
    color: #a5a7a8;
  }
  .login-form-content {
    width: 800px;
    height: 500px;
    display: flex;
    margin: 0 auto;
    margin-top: 200px;
    .login-bg {
      width: 360px;
      height: 100%;
      img {
        font-size: 0;
        width: 100%;
        height: 100%;
      }
    }
  }
}
</style>

<style lang="scss" scoped>
$bg: #34bbff;
$dark_gray: #333;
$light_gray: #333;
.login-container {
  min-height: 100%;
  width: 100%;
  background-color: $bg;
  background: url("../../assets/bg.png") center center no-repeat;
  background-size: cover;
  overflow: hidden;

  .login-form {
    position: relative;
    max-width: 100%;
    overflow: hidden;
    box-sizing: border-box;
  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
    height: 40px;
    line-height: 40px;
    box-sizing: border-box;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 30px;
      color: $light_gray;
      text-align: center;
      font-weight: bold;
      margin-top: 52px;
    }
    .titleEn {
      font-size: 12px;
      color: $light_gray;
      margin: 10px 0;
      text-align: center;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
  .show-pwd-code {
    position: absolute;
    right: 10px;
    font-size: 16px;
    color: #34bbff;
    cursor: pointer;
    user-select: none;
  }
}
.form-rright {
  background: #fff;
  margin: 10px 0;
  color: $light_gray !important;
  flex-direction: column;
  padding: 0 70px;
  box-sizing: border-box;
  width: 440px;
  border-top-right-radius: 8px;
  border-bottom-right-radius: 8px;
}
.el-tabs {
  width: 100%;
  ::v-deep .el-tabs__nav {
    width: 100%;
  }
  ::v-deep .el-tabs__item {
    width: 50% !important;
    color: #a5a7a8 !important;
    padding: 0;
    font-size: 16px;
    text-align: center;
  }
  ::v-deep .el-tabs__active-bar {
    width: 50% !important;
    background: #34bbff;
  }
  ::v-deep .is-active {
    color: #34bbff !important;
  }
  ::v-deep .el-form-item__content {
    height: 38px;
    line-height: 38px;
  }
  ::v-deep .el-input {
    height: 38px;
    line-height: 38px;
    input {
      height: 38px;
      line-height: 38px;
      box-sizing: border-box;
      color: #a5a7a8;
      &:-webkit-autofill {
        -webkit-box-shadow: 0 0 0px 1000px #fff inset !important;
        -webkit-text-fill-color: #7c7e82 !important;
      }
    }
  }
  .svg-icon {
    vertical-align: 0.35em;
  }
}
.color-gray {
  color: #a5a7a8 !important;
}
::v-deep .el-tabs__header {
  margin-bottom: 30px;
}
::v-deep .el-input__inner {
  border: none !important;
}
</style>
